package CustomUI;

import java.util.ArrayList;

import GameTools.ydPoint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
import android.view.View.OnTouchListener;
import android.content.res.Configuration;

public class ydScrollView extends ScrollView implements
OnTouchListener, OnGestureListener {

private static final int SWIPE_MIN_DISTANCE = 30;

private static final int SWIPE_THRESHOLD_VELOCITY = 300;
private static final int SWIPE_PAGE_ON_FACTOR = 10;


private GestureDetector gestureDetector;
private int scrollTo = 0;
private int maxItem = 0;
private int activeItem = 0;
private ydPoint prevScrollX=new ydPoint();
private boolean start = true;
private int itemWidth = 0;
private ydPoint currentScrollX=new ydPoint();
private boolean flingDisable = true;
	
	ArrayList<View> children;

	public ydScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public ydScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	public ydScrollView(Context context) {
		super(context);
		init();
	}
	
	
	//Call init from Activty. Doesnt work from XML build
	public void init(){
		
		children=new ArrayList<View>();
		if(this.getChildCount()>0){
			View v=((ViewGroup)this).getChildAt(0);
			for(int i=0; i<((ViewGroup)v).getChildCount(); ++i) {
			    View nextChild = ((ViewGroup)v).getChildAt(i);
			    children.add(nextChild);
			}
		
		
		  this.maxItem = ((ViewGroup)v).getChildCount();
		  this.itemWidth = ((ViewGroup)v).getChildAt(0).getMeasuredHeight();
		}
		  gestureDetector = new GestureDetector(this);
		  this.setOnTouchListener(this);
		
		
	}
	
	/*public void calculateHeight(){
		this.itemWidth = ((ViewGroup)children.get(0)).getMeasuredHeight();
	}*/
	
	 @Override
	 public boolean onTouch(View v, MotionEvent event) {
		 if(children.size()>0)
		 this.itemWidth = ((ViewGroup)children.get(0)).getMeasuredHeight();
		 else
			 return false;
		 
	  if (gestureDetector.onTouchEvent(event)) {
	   return true;
	  }
	  Boolean returnValue = gestureDetector.onTouchEvent(event);

	  int x = (int) event.getRawX();
	  int y = (int) event.getRawY();

	  switch (event.getAction()) {
	  case MotionEvent.ACTION_MOVE:
	   if (start) {
	    this.prevScrollX.setCoordinates(x, y);
	    start = false;
	    System.out.println("prevScrollX.y : " + prevScrollX.y);
	   }
	   break;
	  case MotionEvent.ACTION_UP:
	   start = true;
	   this.currentScrollX.setCoordinates(x, y);
	   int minFactor = itemWidth / SWIPE_PAGE_ON_FACTOR;

	   if ((this.prevScrollX.y - this.currentScrollX.y) > minFactor) {
	    if (activeItem < maxItem - 1)
	     activeItem = activeItem + 1;

	   } else if ((this.currentScrollX.y - this.prevScrollX.y) > minFactor) {
	    if (activeItem > 0)
	     activeItem = activeItem - 1;
	   }
	   System.out.println("currentScrollX.y : " + currentScrollX.y);
	   System.out.println("vertical : " + activeItem);
	   scrollTo = activeItem * itemWidth;
	   this.smoothScrollTo(0, scrollTo);
	   returnValue = true;
	   break;
	  }
	  return returnValue;
	 }

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	 @Override
	 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
	   float velocityY) {
	  if (flingDisable)
	   return false;
	  boolean returnValue = false;
	  float ptx1 = 0, ptx2 = 0;
	  if (e1 == null || e2 == null)
	   return false;
	  ptx1 = e1.getX();
	  ptx2 = e2.getX();
	  // right to left

	  if (ptx1 - ptx2 > SWIPE_MIN_DISTANCE
	    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
	   if (activeItem < maxItem - 1)
	    activeItem = activeItem + 1;

	   returnValue = true;

	  } else if (ptx2 - ptx1 > SWIPE_MIN_DISTANCE
	    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
	   if (activeItem > 0)
	    activeItem = activeItem - 1;

	   returnValue = true;
	  }
	  scrollTo = activeItem * itemWidth;
	  this.smoothScrollTo(0, scrollTo);
	  return returnValue;
	 }

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}


}
